const list = [
  { id: 'a', pid: '', name: '总裁办' },
  { id: 'b', pid: '', name: '行政部' },
  { id: 'c', pid: '', name: '财务部' },
  { id: 'd', pid: 'c', name: '财务核算部' },
  { id: 'e', pid: 'c', name: '税务管理部' },
  { id: 'f', pid: 'e', name: '税务管理 A 部' },
  { id: 'g', pid: 'e', name: '税务管理 B 部' }
]

const ROOT_ID = ''

function fn1(list, id = ROOT_ID) {
  const arr = []
  list.forEach(item => {
    if (item.pid === id) {
      const children = fn1(list, item.id)
      if (children.length) {
        item.children = children
      }
      arr.push(item)
    }
  })
  return arr
}

const arr = fn1(list)
console.log(arr)
